#include "Camera.h"


Camera::Camera(void)
{
}


Camera::~Camera(void)
{
}

void Camera::Initialize(HWND WindowHandle, HINSTANCE hInstance, ID3D11Device* D3DDevice, ID3D11DeviceContext* D3DDeviceContext)
{
	CameraRotationHorizontal = 0;
	CameraRotationVertical = 0;
	World = DirectX::XMMatrixIdentity();
	Up = DirectX::XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );
	
	x = 0;
	z = -10;
}

void Camera::Update(int DeltaTime)
{
	Eye = DirectX::XMVectorSet( x, 3.0f, z, 0.0f );
	At = DirectX::XMVectorSet( x + sin(CameraRotationHorizontal), 3.0f,z + cos(CameraRotationHorizontal), 0.0f );
	View = DirectX::XMMatrixLookAtLH( Eye, At, Up );
	Projection = DirectX::XMMatrixPerspectiveFovLH( 0.25f*3.14f, 640 / 480.0f, 0.01f, 100.0f );
}

void Camera::Shutdown()
{
	
}

void Camera::MoveForward()
{
		DirectX::XMVECTOR temp = At - Eye;
		temp = DirectX::XMVector4Normalize(temp);	
		x = x + 0.05f*DirectX::XMVectorGetX(temp);
		z = z + 0.05f*DirectX::XMVectorGetZ(temp);
}

void Camera::MoveLeft()
{
		DirectX::XMVECTOR temp = At - Eye;
		DirectX::XMMATRIX rotationMatrix = DirectX::XMMatrixRotationY(0.5f*DirectX::XM_PI);
		DirectX::XMVECTOR Quat = DirectX::XMQuaternionRotationMatrix(rotationMatrix);
		temp = DirectX::XMVector3Rotate(temp, Quat);
		temp = DirectX::XMVector4Normalize(temp);	
		x = x - 0.05f*DirectX::XMVectorGetX(temp);
		z = z - 0.05f*DirectX::XMVectorGetZ(temp);
}

void Camera::MoveBackwards()
{
		DirectX::XMVECTOR temp = At - Eye;
		temp = DirectX::XMVector4Normalize(temp);	
		x = x - 0.05f*DirectX::XMVectorGetX(temp);
		z = z - 0.05f*DirectX::XMVectorGetZ(temp);
}

void Camera::MoveRight()
{
		DirectX::XMVECTOR temp = At - Eye;
		DirectX::XMMATRIX rotationMatrix = DirectX::XMMatrixRotationY(0.5f*DirectX::XM_PI);
		DirectX::XMVECTOR Quat = DirectX::XMQuaternionRotationMatrix(rotationMatrix);
		temp = DirectX::XMVector3Rotate(temp, Quat);
		temp = DirectX::XMVector4Normalize(temp);	
		x = x + 0.05f*DirectX::XMVectorGetX(temp);
		z = z + 0.05f*DirectX::XMVectorGetZ(temp);
}

void Camera::RotateLeft()
{
	CameraRotationHorizontal = CameraRotationHorizontal	+ 0.02f;
}

void Camera::RotateRight()
{
	CameraRotationHorizontal = CameraRotationHorizontal	- 0.02f;
}

DirectX::XMMATRIX Camera::GetProjectionMatrix()
{
	return Projection;
}

DirectX::XMMATRIX Camera::GetViewMatrix()
{
	return View;
}

DirectX::XMMATRIX Camera::GetWorldMatrix()
{
	return World;
}
